import { defineStore } from 'pinia';
import router from "../../Router";

export const useNavigationStore = defineStore('navigation', {
    state: () => ({
        // 定义路由映射关系
        routeMap: {
            home: '/cqust/index',
            teacher: '/cqust/jsjl',
            results: '/cqust/yrcg',
            user: '/cqust/userinfo'
        },
        // 导航历史记录
        history: []
    }),

    actions: {
        // 通用导航方法
        navigateTo(path, record = true) {
            if (record) {
                this.history.push(path);
            }
            router.push(path);
        },

        // 根据名称导航
        goTo(routeName) {
            const path = this.routeMap[routeName] || this.routeMap.home;
            this.navigateTo(path);
        },

        // 返回上一页
        goBack() {
            if (this.history.length > 1) {
                this.history.pop(); // 移除当前页
                const prevPath = this.history.pop(); // 获取上一页
                this.navigateTo(prevPath, false); // 不记录此次导航
            } else {
                this.navigateTo(this.routeMap.home);
            }
        },

        // 特定页面快捷方法
        goToHome() { this.goTo('home'); },
        goToTeacherComm() { this.goTo('teacher'); },
        goToEducationResults() { this.goTo('results'); },
        goToUserCenter() { this.goTo('user'); }
    }
});
